package com.qf.zyh.dao.admin.Impl;

import com.qf.zyh.dao.admin.TypeadDao;
import com.qf.zyh.entity.Type;
import com.qf.zyh.util.DBUtil;
import com.qf.zyh.util.StrUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeadDaoImpl implements TypeadDao {
    QueryRunner qr = new QueryRunner();

    @Override
    public List<Type> seleall() {
        try {
            return qr.query(DBUtil.getConnection(),"select tid,tname,tinfo from type",new BeanListHandler<>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Type> selein(String tname, String tinfo) throws SQLException {
        String sql = "select tid,tname,tinfo from type where 1=1";
        List<Object> list = new ArrayList<>();
        if (StrUtils.empty(tname)) {
            sql=sql+" and tname like ?";
            list.add("%"+tname+"%");
        }
        if (StrUtils.empty(tinfo)) {
            sql=sql+" and tinfo like ?";
            list.add("%"+tinfo+"%");
        }
        return qr.query(DBUtil.getConnection(),sql,new BeanListHandler<>(Type.class),list.toArray());
    }

    @Override
    public void del(Integer tid) {
        try {
            qr.update(DBUtil.getConnection(), "delete from type where tid=?", tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
